package com.cnkvha.trackcontestmgmt.ui;

import com.cnkvha.trackcontestmgmt.UserType;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class PasswordChangeWindow extends JDialog {

    public PasswordChangeWindow(Window parent) {
        super(parent);
        setTitle("选择用户类型并设置密码");

        setSize(600, 240);

        setLayout(new GridLayout(UserType.values().length, 1));

        for(int i = 0; i < UserType.values().length; i++) {
            UserType t = UserType.values()[i];
            String label = String.format("设置 [%s] 的密码", t.getReadableName());
            if(t.getPassword() == null) {
                label += " (未设置!)";
            }
            JButton btn = new JButton(label);
            btn.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    String newPassword = JOptionPane.showInputDialog("请给 [" + t.getReadableName() + "] 设置一个新的密码: ", t.getPassword());
                    t.setPassword(newPassword);
                    dispose();
                }
            });
            add(btn);
        }
    }
}
